package com.example.petplatform.controller;

import com.example.petplatform.common.Result;
import com.example.petplatform.entity.Admin;
import com.example.petplatform.entity.Param.Params;
import com.example.petplatform.entity.Users;
import com.example.petplatform.service.LoginService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/auth")
public class LoginController {

    @Resource
    private LoginService loginService;

    /**
     * 登录接口
     * POST http://localhost:8080/auth/login
     * Content-Type: application/json
     * 
     * 管理员登录请求体示例:
     * {
     *   "username": "admin",
     *   "password": "123456",
     *   "role": "admin"
     * }
     * 
     * 用户登录请求体示例:
     * {
     *   "username": "user1", 
     *   "password": "123456",
     *   "role": "user"
     * }
     */
    @PostMapping("/login")
    public Result Login(@RequestBody Params params) {
        switch (params.getRole()) {
            case "admin":
                Admin admin = loginService.adminLogin(params);
                return Result.success(admin);
            case "user":
                Users user = loginService.userLogin(params);
                return Result.success(user);
            default:
                return Result.error("无效的角色类型");
        }
    }

    /**
     * 用户注册接口
     * POST http://localhost:8080/auth/userregister
     * Content-Type: application/json
     * 
     * 请求体示例:
     * {
     *   "username": "newuser",
     *   "password": "123456",
     *   "name": "张三",
     *   "avatar": "default.jpg",
     *   "role": "user"
     * }
     */
    @PostMapping("/userregister")
    public Result userRegister(@RequestBody Users users) {
        // 添加数据
        Users registeredUser = loginService.userRegister(users);
        return Result.success(registeredUser);
    }
}
